package com.yang.inventorymanagement.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yang.inventorymanagement.pojo.Suppliers;
import com.yang.inventorymanagement.service.SuppliersService;
import com.yang.inventorymanagement.mapper.SuppliersMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author Administrator
 * @description 针对表【suppliers】的数据库操作Service实现
 * @createDate 2023-12-22 09:39:00
 * 供应商信息表
 */
@Service
public class SuppliersServiceImpl extends ServiceImpl<SuppliersMapper, Suppliers>
        implements SuppliersService {



    @Autowired
    private SuppliersMapper suppliersMapper;
    /**
     * 查找所有供应商
     *
     * @return
     */
    @Override
    public Object[][] getAllSuppliers() {

        QueryWrapper<Suppliers> queryWrapper = new QueryWrapper<>();
        queryWrapper.ne("IsDeleted", 1);
        List<Suppliers> suppliers = baseMapper.selectList(queryWrapper);

        if (suppliers != null) {
            Object[][] suppliersMSG = new Object[suppliers.size()][4];
            for (int i = 0; i < suppliers.size(); i++) {
                suppliersMSG[i][0] = suppliers.get(i).getSuppliername();
                suppliersMSG[i][1] = suppliers.get(i).getContactname();
                suppliersMSG[i][2] = suppliers.get(i).getContactemail();
                suppliersMSG[i][3] = suppliers.get(i).getSupplierid();

            }
            return suppliersMSG;
        }
        return new Object[0][0];
    }

    /**
     * 根据供应商名称查询
     */
    @Override
    public Suppliers selectSuppliersBySupplierName(String supplierName) {
        if (supplierName == null) {
            return new Suppliers();
        }

        QueryWrapper<Suppliers> suppliersQueryWrapper = new QueryWrapper<>();
        suppliersQueryWrapper.eq("SupplierName", supplierName);
        Suppliers one = this.getOne(suppliersQueryWrapper);
        return one;
    }

    /**
     * 新增供应商
     *
     * @param suppliers
     * @return
     */
    @Override
    public int insertSuppliers(Suppliers suppliers) {
        if (suppliers == null) {
            return 0;
        }
        //判断供应商名字是否冲突
        QueryWrapper<Suppliers> suppliersQueryWrapper = new QueryWrapper<>();
        suppliersQueryWrapper.eq("SupplierName", suppliers.getSuppliername());
        Suppliers one = this.getOne(suppliersQueryWrapper);
        //供应商存在
        if (one != null) {
            return 0;
        }
        suppliers.setCreatedat(new Date());
        int insert = baseMapper.insert(suppliers);
        return insert;
    }

    /**
     * 修改供应商信息
     *
     * @param suppliers
     * @return
     */
    @Override
    public int updateSuppliersBySupplierID(Suppliers suppliers) {
        if (suppliers == null) {
            return 0;
        }
        int i = baseMapper.updateById(suppliers);
        return i;
    }

    /**
     * 根据id删除供应商 0失败，1成功
     *
     * @param SupplierID
     * @return
     */
    @Override
    public int delSuppliersBySupplierID(String SupplierID) {
        if (SupplierID == null) {
            return 0;
        }
        Suppliers suppliers = new Suppliers();
        suppliers.setSupplierid(Integer.valueOf(SupplierID));
        suppliers.setIsdeleted(1);
        int i = baseMapper.updateById(suppliers);
        return i;
    }

    /**
     * 根据供应商名字
     *
     * @param suppliersName
     * @return
     */
    @Override
    public Object[][] getSearchSuppliers(String suppliersName) {
        if (suppliersName == null) {
            return new Object[0][0];
        }

        QueryWrapper<Suppliers> suppliersQueryWrapper = new QueryWrapper<>();
        suppliersQueryWrapper.like("SupplierName", suppliersName);
        List<Suppliers> suppliers = baseMapper.selectList(suppliersQueryWrapper);


        if (suppliers != null) {
            Object[][] suppliersMSG = new Object[suppliers.size()][4];
            for (int i = 0; i < suppliers.size(); i++) {

                suppliersMSG[i][0] = suppliers.get(i).getSuppliername();
                suppliersMSG[i][1] = suppliers.get(i).getContactname();
                suppliersMSG[i][2] = suppliers.get(i).getContactemail();
                suppliersMSG[i][3] = suppliers.get(i).getSupplierid();


            }
            return suppliersMSG;
        }
        return new Object[0][0];
    }

    @Override
    public List<Suppliers> suppliersGetallList() {
        QueryWrapper<Suppliers> suppliersQueryWrapper = new QueryWrapper<>();
        suppliersQueryWrapper.ne("IsDeleted", 1);
        List<Suppliers> suppliers = baseMapper.selectList(suppliersQueryWrapper);
        return suppliers;
    }


}




